package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;


    ///findAlbumTrackPage/125/1/10
    @TsLogin(required = false)
    @Operation(summary = "根据专辑id分页查询声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
    public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable(name = "albumId") Long albumId,
                                     @PathVariable(name = "pageNo") Long pageNo,
                                     @PathVariable(name = "pageSize") Long pageSize
                                     ){
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNo,pageSize);

        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId,userId);
        //返回数据
        return Result.ok(albumTrackListVoIPage);
    }

    @Operation(summary = "修改声音")
    @PutMapping("updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable("trackId") Long trackId,@RequestBody TrackInfoVo trackInfoVo){
        //调用服务层方法
        trackInfoService.updateTrackInfo(trackId,trackInfoVo);
        return Result.ok();
    }
    @Operation(summary = "根据声音Id获取回显信息")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable("trackId") Long trackId){
        //调用服务层方法
//        TrackInfo trackInfo = trackInfoService.getById(trackId);
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);

        return Result.ok(trackInfo);
    }
    /**
     * 上传声音文件
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传声音文件")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {

        //调用服务层方法
        HashMap<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //获取当前用户id
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        //调用服务层方法
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }

    @Operation(summary = "根据分页查询声音列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable Long pageNo,
                                    @PathVariable Long pageSize,
                                    @RequestBody TrackInfoQuery trackInfoQuery){
        //创建分页对象
        Page<TrackListVo> page = new Page<>(pageNo,pageSize);
        //获取用户Id
        Long userId = AuthContextHolder.getUserId()==null?1l:AuthContextHolder.getUserId();
        //为查询条件设置用户Id
        trackInfoQuery.setUserId(userId);
        IPage<TrackListVo> voPage = trackInfoService.findUserTrackPage(page, trackInfoQuery);
        return Result.ok(voPage);
    }

    /**
     * 根据声音id删除声音
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音id删除声音")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId){
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }
}



